﻿package Component
{
	import Game.Player;
	import Map.Zone;
	import Map.Model.Builder;
	import Map.Block;
	import flash.display.MovieClip;
	import Map.BombPool;
	import Component.BonusPerms.BonusPerm;
	import Map.Bonus;
	import flash.display.DisplayObject;
	import Map.Adjustable;
	import Map.Trap;

	public class Map extends MovieClip
	{

		private var context:Context;

		private var _bomb_pool:BombPool;
		public function get bomb_pool():BombPool
		{
			return _bomb_pool;
		}

		protected var blocks:Vector.<Block > ;
		protected var builders:Vector.<Builder > ;

		// needed by MovieClip DO NOT USE
		public function Map()
		{
			builders = new Vector.<Builder>();
			blocks = new Vector.<Block>();
			_bomb_pool = new BombPool(this);
			
			// we attach all the blocks done by the editor
			for (var i:uint = 0; i < numChildren; i++)
			{
				var child:DisplayObject = getChildAt(i);
				if(child is Adjustable) {
					var adjustable:Adjustable = child as Adjustable;
					adjustable.update();
				}
				if(child is Trap) {
					var trap:Trap = child as Trap;
					trap.bomb_pool = _bomb_pool;
				}
				if(child is Block) {
					//trace(child);
					var block:Block = child as Block;
					blocks.push(block);
				}
			}
		}
		public function initialize(context:Context)
		{
			this.context = context;
		}

		public function move(player_id:uint,x:uint,y:uint):void
		{
			var player:Player = context.match.getPlayer(player_id);
			player.avatar.x = x;
			player.avatar.y = y;
		}

		public function reset()
		{
			// hide all blocks
			for each (var block:Block in blocks)
			{
				block.onMapReset();
			}

			// reset all
			for each (var builder:Builder in builders)
			{
				//trace(builder);
				builder.reset(this);
			}
		}

		// add a block in the map
		// the block is hidden
		public function attach(block:Block):void
		{
			addChild(block);
			blocks.push(block);
		}

		// display a block that 
		public function display(block:Block):void
		{
			block.visible = true;
		}

		public function explode(bomb:Bomb, length:uint)
		{
			trace("re boom");
			// show deflagration
			// we hide the bomb
			bomb.visible = false;
			bomb.explode(length);
		}

		public function set_bomb(x:uint,y:uint):Bomb
		{
			var bomb:Bomb = bomb_pool.grab();
			bomb.setPosition(x,y);
			//attach(bomb);
			display(bomb);
			return bomb;
		}

		public function destroyed(x:uint,y:uint)
		{
			// hide all blocks
			for each (var block:Block in blocks)
			{
				if (block.equals(x,y))
				{
					block.destroy();
				}
			}
		}

		public function set_bonus_perm(x:uint,y:uint)
		{
			for each (var block:Block in blocks)
			{
				if (block.equals(x,y))
				{
					if (block is BonusBlock)
					{
						var bonus_block = block as BonusBlock;
						bonus_block.drop();
						block.destroy();
						return;
					}
				}
			}
		}

		public function getBonus(x:uint,y:uint):Bonus
		{
			for each (var block:Block in blocks)
			{
				if (block.equals(x,y))
				{
					if (block is Bonus)
					{
						return block as Bonus;
					}
				}
			}
			return null;
		}
		
		public function bomb_kicked(x:uint,y:uint,direction:uint) {
			for each (var block:Block in blocks)
			{
				if (block.equals(x,y))
				{
					if (block is Bomb)
					{
						var bomb:Bomb = block as Bomb;
						bomb.kick(direction);
					}
				}
			}
		}
		
		public function getSlab(x:uint,y:uint):Slab {
			for each (var block:Block in blocks)
			{
				if (block.equals(x,y))
				{
					if (block is Slab)
					{
						return(block as Slab);
					}
				}
			}
			return null;
		}
	}

}